import { ref, reactive } from "vue";
import type { Ref } from "vue";
export function useTable<P,R extends {list:any,total:any}>(reqAPI:(params:P)=>RES<R>, reqParams: P) {
  const pageparams = reactive({
    currentPage: 1,
    pageSize: 5,
    total: 1, 
  });
  const tableData: Ref<R["list"]> = ref([]);
  const queryList = async() => {
    // console.log("当前页面:", pageparams.currentPage);
   let res = await reqAPI({
        pageNum: pageparams.currentPage,
        pageSize: pageparams.pageSize,
        ...reqParams
      })
        tableData.value = res.data.list;
        // console.log(tableData.value);
        pageparams.total = res.data.total;
  };
  queryList();
  const handleSizeChange = (val: number) => {
    // console.log("handleSizeChange", val);
    queryList();
  };
  const handleCurrentChange = (val: number) => {
    // console.log("handleCurrentChange", val);
    queryList();
  };

  return {
    tableData,
    pageparams,
    handleSizeChange,
    handleCurrentChange,
    queryList
  }
}
